<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\User;
use app\common\model\designermanager\Designer as DesignerModel;
use app\common\model\designermanager\Production;
use think\Db;
use think\Config;
use fast\Random;

/**
 * 设计师合作接口
 */
class Designer extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     *
     */
    public function index()
    {
        $this->success('', ['welcome' => '123']);
    }

    /*
     * 设计师申请
     */
    public function desiApply(){
        $user_id = $this->auth->getUserinfo()['id'];
        $where['user_id'] = $user_id;
        $allow_apply = DesignerModel::where($where)->field('`status`')->find();
        if($allow_apply){
            if($allow_apply['status'] == 0){
                $this->error('申请等待审核，请勿重复提交');
            }

            if($allow_apply['status'] == 1){
                $this->error('申请已审核通过，请勿重复提交');
            }

            if($allow_apply['status'] == 2){
                $allow_apply->delete();
            }
        }

        $data = $this->request->post();
        $data['user_id'] = $user_id;
        $data['desi_no'] = Random::alnum(10);
        unset($data['token']);
        if(isset($data['share_from'])) unset($data['share_from']);

        $is_success = DesignerModel::create($data);
        $this->success('提交成功', $is_success);
    }

    /*
     * 设计师信息
     */
    public function getDesiInfo(){
        $user_id = $this->auth->getUserinfo()['id'];
        $where['user_id'] = $user_id;
        $where['status'] = array('in', '0, 1');
        $field = 'id, real_name, desi_no, province_id, city_id, county_id, gender, age, years,
            company, qq, mobile, createtime, user_id, status';
        $desi_info = DesignerModel::where($where)->field($field)->find();
        $this->success('设计师信息', $desi_info);
    }

    /*
     * 添加我的印刷
     */
    public function addProduction(){
        $user_id = $this->auth->getUserinfo()['id'];
        $where['user_id'] = $user_id;
        $field = 'id, real_name, desi_no, `status`';
        $desi_info = DesignerModel::where($where)->field($field)->order('id DESC')->find();
        if($desi_info){
            if($desi_info['status'] == 0){
                $this->error('合作申请等待审核中');
            }
            if($desi_info['status'] == 2){
                $this->error('合作申请未通过审核，请联系平台后重新申请');
            }
        }else{
            $this->error('请先提交合作申请');
        }
        $data = $this->request->post();
        $data['user_id'] = $user_id;
        $data['designer_id'] = $data['designer_id'] ? $data['designer_id'] : $desi_info['id'];
        $data['desi_no'] = $data['desi_no'] ? $data['desi_no'] : $desi_info['desi_no'];
        $data['real_name'] = $data['real_name'] ? $data['real_name'] : $desi_info['real_name'];
        $data['prod_no'] = $data['prod_no'] ? $data['prod_no'] : Random::alnum(15);
        //$data['prod_name'] = $data['prod_name'] ? $data['prod_name'] : Random::alnum(15);
        
        $data['finishtime'] = strtotime($data['finishtime']);
        unset($data['token']);

        $is_success = Production::create($data);
        $this->success('提交成功', $is_success);
    }
    /*
     * 查询我的印刷
     */
    public function getProduction(){

        $user_id = $this->auth->getUserinfo()['id'];
        $where['user_id'] = $user_id;

        $org =  DesignerModel::where($where)->find();
        if(!$org || $org->status != 1){
            $this->error("非合作设计师");
        }

        $order = 'id DESC';
        $productionList = Production::where('designer_id', $org->id)->order($order)->paginate()->each(function($item){
            $item->needs_info = $item->needs_info;
        });

        $this->success('我的任务', $productionList);
    }
}
